layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
  var curr_page = 0;
/*初始化表格*/
function tableInit(where = {})
{  
  table.render({
    elem: '#dataTables-example'
    ,url:'/admin/Supplier/supplierlist.html'
	,where:where
    ,method:'POST'
	,parseData: function(res){ //res 即为原始返回的数据
		//封装返回数据
		console.log(res);
		var urls = '';
		curr_page = res.current_page;
		
		for (i in res.data) {
			urls = '/admin/Supplier/editsupplier/id/'+res.data[i]['arid']+'.html';
			res.data[i]['jiaoyi'] = "<button class='layui-btn' onclick=\"xadmin.open('查看详情','"+urls+"',800,500)\">查看详情</button>";;
			urls = '/admin/Supplier/editsupplier/id/'+res.data[i]['supplier_id']+'.html';
			res.data[i]['reason'] = "<button class='layui-btn' onclick=\"xadmin.open('编辑','"+urls+"',800,500)\">编辑</button>";
			res.data[i]['reason'] += "<button class='layui-btn delAdmin' rid='"+res.data[i]['supplier_id']+"' >删除</button>";
		}
		return {
		  "code": 10001, //解析接口状态
		  "msg": '', //解析提示文本
		  "count": res.total, //解析数据长度
		  "data": res.data //解析数据列表
		};
	}
    ,cols: [[
      {field:'supplier_id', title: '序号'}
      ,{field:'supplier_name', title: '供应商姓名'} 
      ,{field:'name', title: '称呼'}
      ,{field:'phone', title: '联系方式'}
      ,{field:'address', title: '地址'}
	  ,{field:'years', title: '合作年限'}
	  ,{field:'jiaoyi', title: '交易记录'}
	  ,{field:'reason', title: '操作'}
    ]]
	,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
      layout: ['prev', 'page', 'next', 'skip', 'count']
	  ,curr:curr_page
    }
	,response: {
		statusCode: 10001
	}
  });
}
tableInit();
  //删除请假
$('body').on('click', '.delAdmin', function(){
	var get_this = $(this);
	parent.layer.confirm('你确定要删除该条信息吗?', function(){
		var params = {};
		params.id = get_this.attr('rid');
		$.ajax({
			type: "POST",
			url: "/admin/Supplier/delsupplier.html",
			cache: false, //禁用缓存
			data: params, //传入组装的参数
			dataType: "json",
			success: function (result) {
				if(result.code==10001){
					var info = {};
					info.sreach = $('#sreach').val();
					tableInit(info);
					parent.layer.msg('操作成功', {icon: 1});
				}else{
					parent.layer.msg(result.msg, {icon: 5});
				}
			},
			error:function () {
				parent.layer.msg('网络错误', {icon: 5});
			}
		})
	});
});
//监听提交
form.on('submit(sreach)',
function(data) {
	tableInit(data.field);
	return false;
});
});

